package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.DisposeLabel;
import com.css.fxfzaqbz.modules.data.entity.DisposeSubtitle;
import com.css.fxfzaqbz.modules.data.repository.DisposeSubtitleRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DisposeSubtitleRepositoryImpl implements DisposeSubtitleRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public void saveDisposeSubtitle(DisposeSubtitle disposeSubtitle) {
        String sql = "insert into sys_dispose_subtitle (id,subtitle,del_flag,create_user,create_time) " +
                "values (:id,:subtitle,:delFlag,:createUser,:createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disposeSubtitle);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public void delDisposeSubtitle(List<String> ids) {
        String sql = "update sys_dispose_subtitle set del_flag = '" + YNEnum.Y + "' where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public Map<String, Object> getDisposeAllSubtitle(Integer curPage, Integer pageSize,DisposeSubtitle disposeSubtitle) {
        StringBuilder sql = new StringBuilder(" select  s.* from sys_dispose_subtitle s ");
        StringBuilder whereSql = new StringBuilder(" where s.del_flag = '" + YNEnum.N.toString() + "'");
        if (!PlatformObjectUtils.isEmpty(disposeSubtitle.getSubtitle())) {
            whereSql.append(" and subtitle like '%" + disposeSubtitle.getSubtitle() + "%'");
        }
        StringBuilder orderSql = new StringBuilder(" order by s.create_time desc");
        int firstIndex=(curPage-1) * pageSize;
        int lastIndex=pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        StringBuilder wholeSql = sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(wholeSql.toString(), new BeanPropertyRowMapper<>(DisposeLabel.class)).size();
        wholeSql.append(limitSql);
        List<DisposeSubtitle> disposeLabels = jdbcTemplate.query(wholeSql.toString(), new BeanPropertyRowMapper<>(DisposeSubtitle.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", disposeLabels);
        return map;
    }

    @Override
    public List<DisposeSubtitle> getBySubtitle(String subtitle) {
        String sql="select * from sys_dispose_subtitle where del_flag='"+YNEnum.N.toString()+"' and subtitle ='"+subtitle+"'";
        List<DisposeSubtitle> disposeLabels = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeSubtitle.class));
        return disposeLabels;
    }
}
